package com.itany.controller;

import com.github.pagehelper.PageInfo;
import com.itany.book.service.UserService;
import com.itany.constant.ResponseConstant;
import com.itany.exception.RequestParamException;
import com.itany.exception.UserNotExistException;
import com.itany.pojo.User;
import com.itany.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * Auther:ws@itany.com
 * Date:20-10-12 下午2:13
 * Version:1.0
 * Description:
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 查询用户
     * @param rows
     * @param page
     * @param username  用户名称
     * @param flag      用户状态
     * @return
     */
    @RequestMapping("/findByParam")
    @ResponseBody
    public Map<String,Object> findByParam(@RequestParam(defaultValue="1") Integer page, @RequestParam(defaultValue = "5") Integer rows, String username, String flag){
        PageInfo<User> userPages = userService.findUserAll(page, rows, username, flag);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",userPages.getTotal());
        map.put("rows",userPages.getList());
        return map;
    }



    /**
     * 修改用户状态
     * @param id        用户id主键
     * @param status    要修改的状态（启用：1, 禁用：2）
     * @return
     */
    @RequestMapping("/modifyStatus")
    @ResponseBody
    public ActionResult modifyStatus(String id, String status){
        ActionResult result = new ActionResult();
        try {
            userService.modifyStatus(id, status);
            result.setStatus(ResponseConstant.RESPONSE_CODE_SUCCESS);
            result.setMsg("成功");
        } catch (RequestParamException e) {
            result.setStatus(ResponseConstant.RESPONSE_CODE_FAIL);
            result.setMsg(e.getMessage());
            e.printStackTrace();
        } catch (UserNotExistException e) {
            result.setStatus(ResponseConstant.RESPONSE_CODE_FAIL);
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return result;
    }


    /**
     * 查询指定用户
     * @param id    用户id主键
     * @return
     */
    @RequestMapping("/findById")
    @ResponseBody
    public ActionResult findById(String id){
        ActionResult result = new ActionResult();

        try {
           User user = userService.findById(id);
           result.setStatus(ResponseConstant.RESPONSE_CODE_SUCCESS);
           result.setData(user);
        } catch (RequestParamException e) {
            result.setStatus(ResponseConstant.RESPONSE_CODE_FAIL);
            result.setMsg(e.getMessage());
            e.printStackTrace();
        } catch (UserNotExistException e) {
            result.setStatus(ResponseConstant.RESPONSE_CODE_FAIL);
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return result;
    }



    /**
     * 修改指定用户信息
     * @param user
     * @return
     */
    @RequestMapping("/modifyUser")
    @ResponseBody
    public ActionResult modifyUser(User user){
        ActionResult result = new ActionResult();
        try {
            userService.modifyUser(user);
            result.setStatus(ResponseConstant.RESPONSE_CODE_SUCCESS);
            result.setMsg("成功");
        } catch (RequestParamException e) {
            e.printStackTrace();
        }

        return result;
    }
}
